﻿using System.Collections.Generic;

/// <summary>
/// 事件基类
/// </summary>
public class ZEvent
{
    /// <summary>
    /// 事件名 广播时以此为依据
    /// </summary>
    public string eventName;
    /// <summary>
    /// 扩展数据类
    /// </summary>
    public object eventObj;

    public ZEvent(string eventName)
    {
        this.eventName = eventName;
    }

    public ZEvent(string eventName, object eventObj)
    {
        this.eventName = eventName;
        this.eventObj = eventObj;
    }
}

/// <summary>
/// 监听者 委托
/// </summary>
/// <param name="ev"></param>
public delegate void EventFunc(ZEvent ev);

/// <summary>
/// 事件广播
/// </summary>
public class EventDispatch
{
    //事件字典
    protected Dictionary<string, List<EventFunc>> dicEvents = new Dictionary<string, List<EventFunc>>();
    /// <summary>
    /// 监听事件
    /// </summary>
    /// <param name="eType"></param>
    /// <param name="eFunc"></param>
    public void AddEventListener(string eType, EventFunc eFunc)
    {
        if (!dicEvents.ContainsKey(eType))
        {
            dicEvents[eType] = new List<EventFunc>();
        }
        if (!dicEvents[eType].Contains(eFunc))
        {
            dicEvents[eType].Add(eFunc);
        }
    }
    /// <summary>
    /// 发送事件
    /// </summary>
    /// <param name="ev"></param>
    public void DispatchEvent(ZEvent ev)
    {
        if (dicEvents.ContainsKey(ev.eventName))
        {
            var funcList = dicEvents[ev.eventName];
            for (int i = 0; i < funcList.Count; i++)
            {
                EventFunc eFunc = funcList[i];
                if (eFunc != null) { eFunc(ev); }
            }
        }
    }
    /// <summary>
    /// 移除监听
    /// </summary>
    /// <param name="eType">事件名字</param>
    /// <param name="eFunc">委托方法</param>
    public void RemoveEventListener(string eType, EventFunc eFunc)
    {
        if (dicEvents.ContainsKey(eType))
        {
            var funcList = dicEvents[eType];
            if (funcList.Contains(eFunc))
            {
                dicEvents[eType].Remove(eFunc);
            }
            if (funcList.Count <= 0)
            {
                dicEvents.Remove(eType);
            }
        }
    }
    /// <summary>
    /// 移除监听
    /// </summary>
    /// <param name="eventType">事件名字</param>
    public void RemoveEventListener(string eventType)
    {
        if (dicEvents.ContainsKey(eventType))
        {
            dicEvents.Remove(eventType);
        }
    }
    /// <summary>
    /// 清空所有事件
    /// </summary>
    public void ClearAllEventListener()
    {
        dicEvents.Clear();
    }
}

/// <summary>
/// 事件管理中心
/// </summary>
public class ZEventCenter
{
    /// <summary>
    /// 事件广播器实例
    /// </summary>
    private static EventDispatch E = new EventDispatch();

    public static void AddEventListener(string eType, EventFunc eFunc)
    {
        E.AddEventListener(eType, eFunc);
    }
    /// <summary>
    /// 更新监听
    /// </summary>
    /// <param name="isAddRem"></param>
    /// <param name="eType"></param>
    /// <param name="eFunc"></param>
    public static void UPEventListener(bool isAddRem, string eType, EventFunc eFunc)
    {
        if (isAddRem)
        {
            E.AddEventListener(eType, eFunc);
        }
        else
        {
            E.RemoveEventListener(eType, eFunc);
        }
    }
    // <summary>
    /// 广播一个事件
    /// </summary>
    /// <param name="ev"></param>
    public static void DispatchEvent(ZEvent ev)
    {
        E.DispatchEvent(ev);
    }
    /// <summary>
    /// 移除事件
    /// </summary>
    /// <param name="eType"></param>
    /// <param name="func"></param>
    public static void RemoveEventListener(string eType, EventFunc eFunc)
    {
        E.RemoveEventListener(eType, eFunc);
    }
    /// <summary>
    /// 移除事件
    /// </summary>
    /// <param name="eventType"></param>
    public static void RemoveEventListener(string eventType)
    {
        E.RemoveEventListener(eventType);
    }
    /// <summary>
    /// 清空所有事件
    /// </summary>
    public static void ClearAllEventListener()
    {
        E.ClearAllEventListener();
    }
}